# How Does Improvement in Commuting Affect Employees? Evidence from a Natural Experiment
# Version: 20211014

## Yao Lu (luyao@sem.tsinghua.edu.cn),        Tsinghua University
## Xinzheng Shi (shixzh@sem.tsinghua.edu.cn), Tsinghua University
## Jagadeesh Sivadasan (jagadees@umich.edu),  University of Michigan
## Zhufeng Xu (zhufeng@zhufengxu.com),        Central University of Finance and Economics

if (!exists("AllScriptFlag", 1)) {
  source("./Scripts/1Header.R")
}

#### Figure 1: Subway Line 15 and Connected Lines
load("Data/Map/beijing_roadmap.RData")
address_map <- read_dta("Data/Map/address_map.dta")
address_map$NearSubway <- factor(address_map$NearSubway, labels = c("Control", "Affected"))
company   <- read_dta("Data/Map/company.dta")
subway15  <- read_dta("Data/Map/subway15.dta")
subway_1  <- read_dta("Data/Map/subway 1.dta")
subway_2  <- read_dta("Data/Map/subway 2.dta")
subway_2  <- bind_rows(subway_2, subway_2[1, ])
subway_4  <- read_dta("Data/Map/subway 4.dta")
subway_5  <- read_dta("Data/Map/subway 5.dta")
subway_6  <- read_dta("Data/Map/subway 6.dta")
subway_7  <- read_dta("Data/Map/subway 7.dta")
subway_8  <- read_dta("Data/Map/subway 8.dta")
subway_9  <- read_dta("Data/Map/subway 9.dta")
subway_10 <- read_dta("Data/Map/subway 10.dta")
subway_10 <- bind_rows(subway_10, subway_10[1, ])
subway_13 <- read_dta("Data/Map/subway 13.dta")
subway_14 <- read_dta("Data/Map/subway 14.dta")
subway_16 <- read_dta("Data/Map/subway 16.dta")

subway_BaTongXian    <- read_dta("Data/Map/subway BaTongXian.dta")
subway_ChangPingXian <- read_dta("Data/Map/subway ChangPingXian.dta")
subway_DaXingXian    <- read_dta("Data/Map/subway DaXingXian.dta")
subway_FangShanXian  <- read_dta("Data/Map/subway FangShanXian.dta")
subway_YanFangXian   <- read_dta("Data/Map/subway YanFangXian.dta")
subway_YiZhuangXian  <- read_dta("Data/Map/subway YiZhuangXian.dta")
subway_S1Xian        <- read_dta("Data/Map/subway S1Xian.dta")
subway_XiJiaoXian    <- read_dta("Data/Map/subway XiJiaoXian.dta")
subway_JiChangXian   <- read_dta("Data/Map/subway JiChangXian.dta")

subway_4 <- bind_rows(subway_4, subway_DaXingXian)

subway <- bind_rows(subway_1, subway_2, subway_4, subway_5, subway_6, subway_7,
                    subway_8, subway_9, subway_10, subway_13, subway_14, subway15, subway_16,
                    subway_BaTongXian, subway_ChangPingXian, subway_DaXingXian, subway_FangShanXian,
                    subway_YanFangXian, subway_YiZhuangXian, subway_S1Xian, subway_XiJiaoXian, subway_JiChangXian)

cols <- c('Subway Line 15' = 'Black', 'Subway Line 5' = 'grey26', 'Subway Line 6' = 'grey26', 'Subway Line 8' = 'grey26',
          'Subway Line 13' = 'grey26', 'Subway Line 14' = 'grey26', 'Subway Line BaTongXian' = 'grey26', 'Subway Line YiZhuangXian' = 'grey26',
          'Other Subway Lines' = 'grey',
          'Plaza' = "Black", 'Control' = "Black", "Affected" = 'Black')
shapes <- c('Subway Line 15' = 24, 'Subway Line 5' = 15, 'Subway Line 6' = 17, 'Subway Line 8'  = 18, 
            'Subway Line 13' = 21, 'Subway Line 14' = 16, 'Subway Line BaTongXian' = 23, 'Subway Line YiZhuangXian' = 0, 
            'Other Subway Lines' = 4,
            'Plaza' = 8, 'Control' = 2, "Affected" = 1)
linetypes <- c('Subway Line 15' = 'solid', 'Subway Line 5' = 'solid', 'Subway Line 6' = 'solid', 'Subway Line 8'  = 'solid',
               'Subway Line 13' = 'solid', 'Subway Line 14' = 'solid', 'Subway Line BaTongXian' = 'solid', 'Subway Line YiZhuangXian' = 'solid',
               'Other Subway Lines' = 'solid',
               'Plaza' = 'blank', 'Control' = 'blank', "Affected" = 'blank')
ggmap(map, extent='device') +
  geom_point(data = subway_1,               aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_1,               aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_2,               aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_2,               aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_4,               aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_4,               aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_7,               aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_7,               aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_9,               aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_9,               aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_10,              aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_10,              aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_16,              aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_16,              aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_ChangPingXian,   aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_ChangPingXian,   aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_DaXingXian,      aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_DaXingXian,      aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_FangShanXian,    aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_FangShanXian,    aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_YanFangXian,     aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_YanFangXian,     aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_S1Xian,          aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_S1Xian,          aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_XiJiaoXian,      aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_XiJiaoXian,      aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_JiChangXian,     aes(x = lng, y = lat, colour = 'Other Subway Lines', shape    = 'Other Subway Lines'), stroke = 1.4, size  = 2.5, alpha = 1) +
  geom_path( data = subway_JiChangXian,     aes(x = lng, y = lat, colour = 'Other Subway Lines', linetype = 'Other Subway Lines'), size   = 2.0, alpha = 1) +
  geom_point(data = subway15,               aes(x = lng, y = lat, colour   = 'Subway Line 15', shape  = 'Subway Line 15'), stroke = 1.4, size  = 3.5, alpha = 1) +
  geom_path( data = subway15,               aes(x = lng, y = lat, linetype = 'Subway Line 15', colour = 'Subway Line 15'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_5,               aes(x = lng, y = lat, colour   = 'Subway Line 5',  shape  = 'Subway Line 5'),  size   = 3.5, alpha = 1) +
  geom_path( data = subway_5,               aes(x = lng, y = lat, linetype = 'Subway Line 5',  colour = 'Subway Line 5'),  size   = 2.0, alpha = 1) +
  geom_point(data = subway_6,               aes(x = lng, y = lat, colour   = 'Subway Line 6',  shape  = 'Subway Line 6'),  size   = 3.5, alpha = 1) +
  geom_path( data = subway_6,               aes(x = lng, y = lat, linetype = 'Subway Line 6',  colour = 'Subway Line 6'),  size   = 2.0, alpha = 1) +
  geom_point(data = subway_8,               aes(x = lng, y = lat, colour   = 'Subway Line 8',  shape  = 'Subway Line 8'),  size   = 3.5, alpha = 1) +
  geom_path( data = subway_8,               aes(x = lng, y = lat, linetype = 'Subway Line 8',  colour = 'Subway Line 8'),  size   = 2.0, alpha = 1) +
  geom_point(data = subway_13,              aes(x = lng, y = lat, colour   = 'Subway Line 13', shape  = 'Subway Line 13'), stroke = 1.4, size  = 3.5, alpha = 1) +
  geom_path( data = subway_13,              aes(x = lng, y = lat, linetype = 'Subway Line 13', colour = 'Subway Line 13'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_14,              aes(x = lng, y = lat, colour   = 'Subway Line 14', shape  = 'Subway Line 14'), size   = 3.5, alpha = 1) +
  geom_path( data = subway_14,              aes(x = lng, y = lat, linetype = 'Subway Line 14', colour = 'Subway Line 14'), size   = 2.0, alpha = 1) +
  geom_point(data = subway_BaTongXian,      aes(x = lng, y = lat, colour   = 'Subway Line BaTongXian',   shape  = 'Subway Line BaTongXian'),   stroke = 1.4, size  = 3.5, alpha = 1) +
  geom_path( data = subway_BaTongXian,      aes(x = lng, y = lat, linetype = 'Subway Line BaTongXian',   colour = 'Subway Line BaTongXian'),   size   = 3.0, alpha = 1) +
  geom_point(data = subway_YiZhuangXian,    aes(x = lng, y = lat, colour   = 'Subway Line YiZhuangXian', shape  = 'Subway Line YiZhuangXian'), stroke = 1.4, size  = 3.5, alpha = 1) +
  geom_path( data = subway_YiZhuangXian,    aes(x = lng, y = lat, linetype = 'Subway Line YiZhuangXian', colour = 'Subway Line YiZhuangXian'), size   = 2.0, alpha = 1) +
  geom_point(data = company,     aes(x = lng, y = lat, shape = 'Plaza', color = 'Plaza'), stroke = 2.4, size=6.0, alpha = 1, show.legend = FALSE) +
  geom_point(data = address_map, aes(x = lng, y = lat, shape = NearSubway, colour = NearSubway), size = 2.0, show.legend = FALSE) +
  scale_shape_manual(name="Plaza, Homes and Subways", values = shapes, 
                     limits=c('Plaza', 'Affected', 'Control',  'Subway Line 15', 'Subway Line 5', 'Subway Line 6', 'Subway Line 8', 'Subway Line 13',
                              'Subway Line 14', 'Subway Line BaTongXian', 'Subway Line YiZhuangXian', 'Other Subway Lines')) +
  scale_linetype_manual(name = 'Plaza, Homes and Subways', values = linetypes,
                        limits=c('Plaza', 'Affected', 'Control', 'Subway Line 15', 'Subway Line 5', 'Subway Line 6', 'Subway Line 8', 'Subway Line 13',
                                 'Subway Line 14', 'Subway Line BaTongXian', 'Subway Line YiZhuangXian', 'Other Subway Lines')) +
  scale_colour_manual(name = 'Plaza, Homes and Subways', values = cols,
                      limits=c('Plaza', 'Affected', 'Control', 'Subway Line 15', 'Subway Line 5', 'Subway Line 6', 'Subway Line 8', 'Subway Line 13',
                               'Subway Line 14', 'Subway Line BaTongXian', 'Subway Line YiZhuangXian', 'Other Subway Lines')) +
  ylim(max(min(subway$lat), attr(map, "bb")$ll.lat), min(max(subway$lat), attr(map, "bb")$ur.lat)) +
  xlim(max(min(subway$lng), attr(map, "bb")$ll.lon), min(max(subway$lng), attr(map, "bb")$ur.lon)) +
  theme(text = element_text(family = "Times New Roman", size = 12, lineheight = 2))
ggsave("Output/Figure1.pdf", width = 11, height = 8.5, unit = "in", device = cairo_pdf)
ggsave("Output/Figure1.png", width = 11, height = 8.5, unit = "in")

rm(map, address_map, company, subway, subway15, subway_1, subway_2, subway_4, subway_5, subway_6,
   subway_7, subway_8, subway_9, subway_10, subway_13, subway_14, subway_16,
   subway_BaTongXian, subway_ChangPingXian, subway_DaXingXian, subway_FangShanXian,
   subway_JiChangXian, subway_S1Xian, subway_XiJiaoXian, subway_YanFangXian, subway_YiZhuangXian,
   cols, linetypes, shapes)
